var barqrGen = require('../../../utils/wxbarqrGen.js');
var allcoupon;
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponlist: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var cptype = '1';
    if (options.cptype) {
      cptype = options.cptype;
    }
    this.setData({
      cptype: cptype
    });
    var that = this;
    wx.getSystemInfo({
      success: function(res) {
        var iconhgt = (res.windowWidth - 20) * 252 / 681;
        that.setData({
          iconhgt: iconhgt
        });
      },
    });
    
    this.createBarcode();
    wx.showLoading({
      title: '',
    });
    this.loadCouponlist();
  },
  loadCouponlist: function () {
    var url = '/yuanmai_we/activity/getpromotion/list';
    app.loadDataFromServer(url, this.loadCouponlistCallBack, {}, 'get', true);
  },
  loadCouponlistCallBack: function(res) {
    console.log(res);
    wx.hideLoading();
    allcoupon = res;
    for (var keystr in allcoupon) {
      var temp = '';
      for (var i = 0; i < allcoupon[keystr].length; i++) {
        temp = allcoupon[keystr][i].STARTTIME;
        allcoupon[keystr][i].STARTTIME = temp.split(' ')[0];
        temp = allcoupon[keystr][i].ENDTIME;
        allcoupon[keystr][i].ENDTIME = temp.split(' ')[0];
      }
    }
    this.dealCouponlist(this.data.cptype);
  },
  dealCouponlist: function(cptype) {
    var temp_list = [];
    var arr;
    if (cptype == '1') {
      arr = allcoupon.EXPRESS;
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].STATUS == "UNUSED") {
          temp_list.push(arr[i])
        }
      }
      this.setData({
        couponlist: temp_list
      });
    }
    else if(cptype == '2'){
      arr = allcoupon.OFFLINE;
      for (var i = 0; i < arr.length; i++) {
        if (arr[i].STATUS == "UNUSED") {
          temp_list.push(arr[i])
        }
      }
      this.setData({
        offlinelist: temp_list
      });
      for (var i = 0; i < temp_list.length; i++) {
        var barcodeid = "bq"+i;
        barqrGen.barcode(barcodeid, app.globalData.cardNO, 560, 240);
      }
    }
    else if(cptype == '3'){
      arr = allcoupon;
      for(var keystr in arr){
        for(var i=0;i<arr[keystr].length;i++){
          if (arr[keystr][i].STATUS == 'EXPIRED'){
            arr[keystr][i].ctype = keystr;
            if (keystr == 'SKU') {
              arr[keystr][i].ctype = 'EXPRESS';
            }
            temp_list.push(arr[keystr][i]);
          }
        }
      }
      this.setData({
        couponlist: temp_list
      });
    }
    else if(cptype == '4'){
      arr = allcoupon;
      for (var keystr in arr) {
        for (var i = 0; i < arr[keystr].length; i++) {
          if (arr[keystr][i].STATUS == 'USED') {
            arr[keystr][i].ctype = keystr;
            if(keystr == 'SKU'){
              arr[keystr][i].ctype = 'EXPRESS';
            }
            temp_list.push(arr[keystr][i]);
          }
        }
      }
      this.setData({
        couponlist: temp_list
      });
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  changeCouponType: function(e) {
    var cptype = e.currentTarget.id;
    if (cptype == this.data.cptype) {
      return;
    }
    this.setData({
      cptype: cptype
    });

    this.dealCouponlist(this.data.cptype);
  },
  createBarcode:function(){
    var cardno = app.globalData.cardNO;
    this.setData({
      cardNo: cardno
    });
  },
  useOfflineCoupon:function(e){
    var couponid = e.currentTarget.dataset.couponid;
    var url = '/yuanmai_we/activity/offline/usePromotion';
    var property = {
      ID: couponid
    };
    var params = {
      datas: JSON.stringify(property),
      __zaofans: true
    }
    app.loadDataFromServer(url, this.useOfflineCouponCallBack,params,'post',true);
  },
  useOfflineCouponCallBack:function(res){
    console.log(res);
    if (res.result == 'SUCCESS'){
      wx.showToast({
        title: '兑换成功',
        duration: 1000,
        icon: 'success'
      });
      app.getUserInfo();
      var that = this;
      var delayid = setTimeout(function(){
        clearTimeout(delayid);
        that.loadCouponlist();
        app.globalData.needUpdateUserInfo = true;
      },1000);
    }
  }
})